package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.common.util.CovertUtil;
import com.dragon.logical.service.auth.IDictsortDetailService;
import com.dragon.repository.entity.DictsortDetail;
import com.dragon.repository.mapper.DictsortDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 类型键值表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-06-27
 */
@Service
public class DictsortDetailServiceImpl extends ServiceImpl<DictsortDetailMapper, DictsortDetail> implements IDictsortDetailService {


    @Autowired
    DictsortDetailMapper mapper;

    @Override
    public Page<DictsortDetail> selectPageList(String code,Integer page, Integer limit){
        Page<DictsortDetail> detailPage = new Page<>(page,limit);
        QueryWrapper<DictsortDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DictsortDetail::getCodeType,code);
        Page<DictsortDetail> dictsortDetailPage = mapper.selectPage(detailPage,queryWrapper);
        return dictsortDetailPage;
    }

    @Override
    public Integer addDetail(DictsortDetail detail){
        detail.setCreateTime(DateUtil.date());
        mapper.insert(detail);
        return detail.getId();
    }

    @Override
    public void updateDetail(DictsortDetail detail){
        DictsortDetail detail1 = mapper.selectById(detail.getId());
        if(detail1 == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        detail.setUpdateTime(DateUtil.date());
        mapper.updateById(detail);
    }

    @Override
    public void deleteDetail(Integer id){
        DictsortDetail detail = mapper.selectById(id);
        if(detail == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        mapper.deleteById(detail);
    }

    @Override
    public void deleteBatchDetail(String ids){
        List<Integer> idList = CovertUtil.covertIntegerList(ids);
        QueryWrapper<DictsortDetail> queryWrapper = new QueryWrapper<>();
        if(idList.size()!=0){
            queryWrapper.lambda().in(DictsortDetail::getId,idList);
        }
        mapper.delete(queryWrapper);
    }

    @Override
    public List<DictsortDetail> selectDictSortDetail(String code){
        QueryWrapper<DictsortDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DictsortDetail::getCodeType,code);
        return mapper.selectList(queryWrapper);
    }

    @Override
    public DictsortDetail selectNameByCodeAndValue(String code, Integer value){
        QueryWrapper<DictsortDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(DictsortDetail::getCodeType,code)
                .eq(DictsortDetail::getValue,value)
                .eq(DictsortDetail::getStatus,0);
        DictsortDetail dictsortDetail = mapper.selectOne(queryWrapper);
        if(dictsortDetail==null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        return dictsortDetail;
    }
}
